Imports cmplib01
Imports cmplib01.CampaignerGameData


Public Class ctlCombatsTreeview
    Inherits System.Windows.Forms.UserControl

    Public Event BattleSelected(ByRef batt As Battle)

    Private ptrGameData As CampaignerGameData
    Private m_SelectedBattle As Battle

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'UserControl overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents tvwCombats As System.Windows.Forms.TreeView
    Friend WithEvents imgCheckNoCheck As System.Windows.Forms.ImageList
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.components = New System.ComponentModel.Container
        Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(ctlCombatsTreeview))
        Me.tvwCombats = New System.Windows.Forms.TreeView
        Me.imgCheckNoCheck = New System.Windows.Forms.ImageList(Me.components)
        Me.SuspendLayout()
        '
        'tvwCombats
        '
        Me.tvwCombats.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.tvwCombats.Dock = System.Windows.Forms.DockStyle.Fill
        Me.tvwCombats.ImageList = Me.imgCheckNoCheck
        Me.tvwCombats.Location = New System.Drawing.Point(0, 0)
        Me.tvwCombats.Name = "tvwCombats"
        Me.tvwCombats.Nodes.AddRange(New System.Windows.Forms.TreeNode() {New System.Windows.Forms.TreeNode("1st Battalion vs Blackgutz Warmob", New System.Windows.Forms.TreeNode() {New System.Windows.Forms.TreeNode("Attacker: 1st Battalion (Micah)"), New System.Windows.Forms.TreeNode("Defender: Blackgutz Warmob (Tim)"), New System.Windows.Forms.TreeNode("Result: Unresolved")})})
        Me.tvwCombats.Size = New System.Drawing.Size(224, 160)
        Me.tvwCombats.TabIndex = 3
        '
        'imgCheckNoCheck
        '
        Me.imgCheckNoCheck.ImageSize = New System.Drawing.Size(16, 16)
        Me.imgCheckNoCheck.ImageStream = CType(resources.GetObject("imgCheckNoCheck.ImageStream"), System.Windows.Forms.ImageListStreamer)
        Me.imgCheckNoCheck.TransparentColor = System.Drawing.Color.Transparent
        '
        'ctlCombatsTreeview
        '
        Me.Controls.Add(Me.tvwCombats)
        Me.Name = "ctlCombatsTreeview"
        Me.Size = New System.Drawing.Size(224, 160)
        Me.ResumeLayout(False)

    End Sub

#End Region

#Region "properties"
    Public Property GameData() As CampaignerGameData
        Get
            Return ptrGameData
        End Get
        Set(ByVal Value As CampaignerGameData)
            ptrGameData = Value
            ListBattles()
        End Set
    End Property

    Public Property SelectedBattle() As Battle
        Get
            Return m_SelectedBattle
        End Get
        Set(ByVal Value As Battle)
            m_SelectedBattle = Value
        End Set
    End Property

#End Region

    Private Sub ListBattles()
        Try
            ClearAll()

            If ptrGameData Is Nothing Then                Exit Sub

            '// lets create each battle and populate it, then ensure all nodes invisible.
            Dim Batt As Battle
            Dim battKey As String
            For Each battKey In ptrGameData.Battles_g.HT.Keys
                Batt = ptrGameData.Battles_g.Item(battKey)
                SetupBattle(Batt)
            Next
        Catch ex As Exception
            HandleException("ctlCombatsTreeview.ListBattles", ex)
        End Try
    End Sub

    Private Sub SetupBattle(ByRef Batt As Battle)
        Try
            Dim battNode As New BattleTreeNode(Batt.BattleName)
            battNode.BattleID = Batt.BattleID

            If Batt.BattleResult = BattleResult_cgd.NotFinished Then
                battNode.ImageIndex = 0
            Else
                battNode.ImageIndex = 1
            End If
            '// add the attacker node
            Dim atNode As New TreeNode
            atNode.Text = ptrGameData.Faction_g.Item(CDGKEY_FACTION & Batt.AttackingFactionID).FactionName
            atNode.ImageIndex = 2
            battNode.Nodes.Add(atNode)
            '// add the defender node
            Dim deNode As New TreeNode
            deNode.Text = ptrGameData.Faction_g.Item(CDGKEY_FACTION & Batt.DefendingFactionID).FactionName
            deNode.ImageIndex = 2
            battNode.Nodes.Add(deNode)
            'deNode.EnsureVisible()
            '// add the result node
            Dim reNode As New TreeNode
            reNode.Text = Batt.BattleResultString
            reNode.ImageIndex = 3
            battNode.Nodes.Add(reNode)
            tvwCombats.Nodes.Add(battNode)
        Catch ex As Exception
            HandleException("ctlCombatsTreeview.SetupBattle", ex)
        End Try
    End Sub

    Private Sub ClearAll()
        Try
            tvwCombats.Nodes.Clear()
        Catch ex As Exception

        End Try
    End Sub


    Private Sub tvwCombats_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles tvwCombats.AfterSelect
        Dim bNode As BattleTreeNode

        Try
            If TypeOf e.Node Is BattleTreeNode Then
                bNode = e.Node
            Else
                bNode = e.Node.Parent
            End If
            '// now select the battle
            m_SelectedBattle = ptrGameData.Battles_g.Item(CDGKEY_BATTLE & bNode.BattleID)
            RaiseEvent BattleSelected(m_SelectedBattle)
        Catch ex As Exception
            HandleException("ctlCombatsTreeview.tvwCombats.AfterSelect", ex)
        End Try


    End Sub
End Class

